<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use think\Hook;

/*主体内容部分*/
class Main extends AdminBase
{
	public function _initialize(){
    	$filter=array("login","login_post","login_out","lock_varify","lock");
    	if(!in_array(request()->action(),$filter)){
    		parent::_initialize();
    	}
    }
	  
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	
	
	
	public function lock(){
		$this->assign("web_site",config('company.web_site'));
		if(empty(config("database.asyn_status"))){
			return $this->fetch("./public/admin/lock.html");
			die;
		}
		return $this->fetch("./public/admin/login.html");
	}	
	
	public function index(){
		$result = Hook::exec('app\\admin\\behavior\\CheckAuth','actionInit');
		//var_dump($result);die;
		return $this->fetch();
	}
	
		/*验证*/
	public function lock_varify(){
		$this->_lock_varify();
	}
	/*登录页面*/
	public function login(){
		$this->assign("web_site",config('company.web_site'));
		if(empty(config("database.asyn_status"))){
			return $this->fetch("./public/admin/lock.html");
			die;
		}
		asyncronous(config("database.back_url"));	
//		if(input("type")=="auto"){//自动登陆
//			$admin=db("admin")->where("account='shop_test'")->fetchsql(FALSE)->find();
//			session("Admin",$admin);
//			$this->redirect('Main/index');
//		}
		return $this->fetch("./public/admin/login.html");
	}
	/*登录post*/
	public function login_post(){
		$this->_login_post();
	}
	/*退出*/
	public function login_out(){
		session(null);
		$this->redirect('login');
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu();
		$this->assign("menus",$auth_group);
		$this->assign("web_site",config('company.web_site'));
		$this->assign("admin",sp_get_current_admin());
	}
	function _login_post(){
		if(empty(input('account'))||empty(input('psw'))){
			$this->ajax_error('参数错误！');
		}else{
			$account=input('account/s');
			$psw=md5(input('psw/s'));
			$code=input('code/s');
			
				$admin=db("admin")->where("account='$account' and password='$psw'")->fetchsql(FALSE)->find();
				if(empty($admin)){
					$this->ajax_error("账号或者密码错误！");
				}else{
					session("Admin",$admin);
					db("AdminLog")->insert(array("admin_id"=>$admin['id'],"ip"=>request()->ip()));
					$this->ajax_success("验证通过！");
				}
		}
	}
		function _lock_varify(){
		$shop_name=input("shop_name");
		$shop_id=input("shop_id");
		$sql_dir=input("sql_dir");
		if(empty($shop_name)||empty($shop_id)||empty($sql_dir)){
			$this->ajax_error('参数错误！');
		}else{
			if(!file_exists($sql_dir)){
				$this->ajax_error("sql路径错误！");
			}
//			if(count(scandir($sql_dir))<3){
//				$this->ajax_error("请先通过navicat备份sql");
//			}
			$client = new \GuzzleHttp\Client();
			$response=$client->request('POST', config("database.asyn_base").'Admin/Shop/shop_info',['multipart' => [['name'=>"shop_name","contents"=>$shop_name],['name'=>"shop_id","contents"=>$shop_id]]])->getBody()->getContents();;
			$response=json_decode($response,TRUE);
			if($response['flag']){
				sp_dynamic_database(array("asyn_status"=>TRUE,"shop_id"=>$shop_id,"shop_name"=>$shop_name,"sql_dir"=>trim(str_replace('\\', "/", $sql_dir))));
				$this->ajax_success("成功");
			}else{
				$this->ajax_error($response['msg']);	
			}
		}
	}
}
